package com.wnxy.admin.common.util;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.admin.common.handler.ChargingPileStateEnum;
import com.wnxy.admin.common.handler.OrderStatusEnum;
import com.wnxy.admin.common.handler.ReservationEnum;
import com.wnxy.admin.dto.OrderDto;
import com.wnxy.admin.entity.ChargingCard;
import com.wnxy.admin.entity.Order;
import com.wnxy.admin.entity.Reservation;
import com.wnxy.admin.mapper.OrderMapper;
import com.wnxy.admin.service.IChargingCardService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import com.wnxy.admin.service.IChargingPileService;
import com.wnxy.admin.service.ICustomerService;
import com.wnxy.admin.service.IReservationService;
import com.wnxy.admin.service.IChargingPileService;
import com.wnxy.admin.service.ICustomerService;
import com.wnxy.admin.service.IReservationService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Component
public class OrderUtil extends ServiceImpl<OrderMapper, Order> {

    private final OrderMapper orderMapper;
    private final IChargingCardService chargingCardServiceImpl;
    private final RedisTemplate redisTemplate;

    private final IChargingPileService chargingPileServiceImpl;
    private final ICustomerService customerServiceImpl;
    private final IReservationService reservationServiceImpl;

    public OrderUtil(OrderMapper orderMapper, IChargingCardService chargingCardServiceImpl, RedisTemplate redisTemplate, IChargingPileService chargingPileServiceImpl, ICustomerService customerServiceImpl, IReservationService reservationServiceImpl) {
        this.orderMapper = orderMapper;
        this.chargingCardServiceImpl = chargingCardServiceImpl;
        this.redisTemplate = redisTemplate;
        this.chargingPileServiceImpl = chargingPileServiceImpl;
        this.customerServiceImpl = customerServiceImpl;
        this.reservationServiceImpl = reservationServiceImpl;
    }

    /*
     * @author LQC
     * @note 完成预约状态
     * @Help: 订单Order
     */
    public void done(Reservation reservation) {
        reservationServiceImpl.update(Wrappers.lambdaUpdate(Reservation.class)
                .eq(Reservation::getId, reservation.getId())
                .set(Reservation::getState, ReservationEnum.DONE.getCode()));

        // TODO: 2023/12/2 修改客户状态
        customerServiceImpl.editService(reservation, 0);

        // TODO: 2023/12/2 修改电桩状态
        Long chargingId = Long.valueOf(reservation.getChargingId());
        chargingPileServiceImpl.updatePileStateById(chargingId, ChargingPileStateEnum.PIE_DEFAULT.getCode());
    }

    /*
     * @author (^･ｪ･^)Cat~
     * @查询业务订单api
     * @Help: 预约Reservation
     */
    public List<Order> queryNotOkOrderByCustomerId(Integer customerId) {
        List<Order> orders = orderMapper.selectList(Wrappers.lambdaQuery(Order.class)
                .eq(Order::getCustomerId, customerId)
                .ne(Order::getTransactionStatus, OrderStatusEnum.SUCCESS_PAY.getCode()));
        return orders;
    }

    public Order generateOrder(OrderDto orderDto) {
        Order order = new Order();
        if (orderDto.getCardId() != null && orderDto.getCardId() != 0) {
            ChargingCard chargingCard = chargingCardServiceImpl.queryChargingCardDetail(orderDto.getCardId());

            orderDto.setCardNumber(chargingCard.getChargingCardNumber());
            orderDto.setCustomerId(Math.toIntExact(chargingCard.getCustomerId()));
        }

        // 调用BeanUtil进行属性拷贝
        BeanUtil.copyProperties(orderDto, order);
        order.setPaymentMethod(orderDto.getCardNumber());
        return order;
    }

    public Order getOrder4Redis(String cardNumber) {
        HashOperations<String, String, Order> hashOperations = redisTemplate.opsForHash();
        Map<String, Order> entries = hashOperations.entries("Order");

        Object tempOrder = entries.get(cardNumber);
        Order order = new Order();
        BeanUtil.copyProperties(tempOrder, order);
        return order;
    }

}
